{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "tv1pv7HITr8Z"
   },
   "source": [
    "# Torrent to Google Drive"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "OT2A4UfMT13u"
   },
   "source": [
    "### We will use Google Colab and python3-libtorrent"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "s4TwJU-pVNC9"
   },
   "source": [
    "#### Imports\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 34
    },
    "id": "xAcisUTDVTBj",
    "outputId": "6b1d6b1f-4206-4277-fa20-28bce1744181"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount(\"/content/drive\", force_remount=True).\n"
     ]
    }
   ],
   "source": [
    "from google.colab import drive\n",
    "drive.mount('/content/drive')\n",
    "import libtorrent as lt\n",
    "import time\n",
    "import datetime\n",
    "\n",
    "# Follow the URL select the required Google Account\n",
    "# paste the provided Authentication Code"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "s1DhrbkZWL1P"
   },
   "source": [
    "#### Install Dependency (python3-libtorrent)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 492
    },
    "id": "102SJB0mV1YR",
    "outputId": "63c01bea-9b94-47cb-c2a9-a8089313420b"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Reading package lists... Done\n",
      "Building dependency tree       \n",
      "Reading state information... Done\n",
      "The following additional packages will be installed:\n",
      "  libtorrent-rasterbar9\n",
      "Suggested packages:\n",
      "  libtorrent-rasterbar-dbg\n",
      "The following NEW packages will be installed:\n",
      "  libtorrent-rasterbar9 python3-libtorrent\n",
      "0 upgraded, 2 newly installed, 0 to remove and 22 not upgraded.\n",
      "Need to get 1,569 kB of archives.\n",
      "After this operation, 5,718 kB of additional disk space will be used.\n",
      "Get:1 http://archive.ubuntu.com/ubuntu bionic/universe amd64 libtorrent-rasterbar9 amd64 1.1.5-1build1 [1,258 kB]\n",
      "Get:2 http://archive.ubuntu.com/ubuntu bionic/universe amd64 python3-libtorrent amd64 1.1.5-1build1 [310 kB]\n",
      "Fetched 1,569 kB in 1s (1,811 kB/s)\n",
      "Selecting previously unselected package libtorrent-rasterbar9.\n",
      "(Reading database ... 144618 files and directories currently installed.)\n",
      "Preparing to unpack .../libtorrent-rasterbar9_1.1.5-1build1_amd64.deb ...\n",
      "Unpacking libtorrent-rasterbar9 (1.1.5-1build1) ...\n",
      "Selecting previously unselected package python3-libtorrent.\n",
      "Preparing to unpack .../python3-libtorrent_1.1.5-1build1_amd64.deb ...\n",
      "Unpacking python3-libtorrent (1.1.5-1build1) ...\n",
      "Setting up libtorrent-rasterbar9 (1.1.5-1build1) ...\n",
      "Setting up python3-libtorrent (1.1.5-1build1) ...\n",
      "Processing triggers for libc-bin (2.27-3ubuntu1.2) ...\n",
      "/sbin/ldconfig.real: /usr/local/lib/python3.6/dist-packages/ideep4py/lib/libmkldnn.so.0 is not a symbolic link\n",
      "\n"
     ]
    }
   ],
   "source": [
    "!apt install python3-libtorrent"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "Rh5kBbj_WrxB"
   },
   "source": [
    "After Successful Installation, search the required Torrent in any Torrent Website. Ricght click on the Magnet Link, Copy the Address. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "Pa57pbwvXew7"
   },
   "source": [
    "#### Start the Torrent Sesssion"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "rz8go4QIXm9d"
   },
   "source": [
    "Torrent Used in this [Example](https://www.1377x.to/torrent/4634768/MediaMonkey-Gold-4-1-30-1913-x86-x64-Multilingual-Keygen-FTUApps/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 54
    },
    "id": "Q3ie9kbOWmjk",
    "outputId": "d2891ce8-c818-4059-84e6-3fd8f910a396"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "magnet:?xt=urn:btih:71EB7E8DBF3480B251A27D26099898E78FAF30D9&dn=MediaMonkey+Gold+4.1.30.1913+%28x86+%26amp%3B+x64%29+Multilingual+%2B+Keygen+%5BFTUApps%5D&tr=udp%3A%2F%2Fopentor.org%3A2710%2Fannounce&tr=udp%3A%2F%2Fp4p.arenabg.com%3A1337%2Fannounce&tr=udp%3A%2F%2Ftracker.torrent.eu.org%3A451%2Fannounce&tr=udp%3A%2F%2Ftracker.cyberia.is%3A6969%2Fannounce&tr=udp%3A%2F%2F9.rarbg.to%3A2710%2Fannounce&tr=udp%3A%2F%2Fexodus.desync.com%3A6969%2Fannounce&tr=udp%3A%2F%2Fexplodie.org%3A6969%2Fannounce&tr=udp%3A%2F%2Ftracker.moeking.me%3A6969%2Fannounce&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337%2Fannounce&tr=udp%3A%2F%2Ftracker.tiny-vps.com%3A6969%2Fannounce&tr=udp%3A%2F%2Fipv4.tracker.harry.lu%3A80%2Fannounce&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969%2Fannounce&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969%2Fannounce&tr=udp%3A%2F%2Fopen.stealth.si%3A80%2Fannounce&tr=udp%3A%2F%2Ftracker.zer0day.to%3A1337%2Fannounce&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969%2Fannounce&tr=udp%3A%2F%2Fcoppersurfer.tk%3A6969%2Fannounce\n"
     ]
    }
   ],
   "source": [
    "ses = lt.session()\n",
    "ses.listen_on(6881, 6891)\n",
    "params = {\n",
    "    'save_path': '/content/drive/My Drive/Torrent/', #Enter the Destination Path here \n",
    "    #By default Torrent File would be saved in a folder \"Torrent\" in selected Drive\n",
    "    'storage_mode': lt.storage_mode_t(2), # Use Sparse Storage Mode\n",
    "    'paused': False,\n",
    "    'auto_managed': True,\n",
    "    'duplicate_is_error': True}\n",
    "# Magnet Link Goes Here\n",
    "link = \"magnet:?xt=urn:btih:71EB7E8DBF3480B251A27D26099898E78FAF30D9&dn=MediaMonkey+Gold+4.1.30.1913+%28x86+%26amp%3B+x64%29+Multilingual+%2B+Keygen+%5BFTUApps%5D&tr=udp%3A%2F%2Fopentor.org%3A2710%2Fannounce&tr=udp%3A%2F%2Fp4p.arenabg.com%3A1337%2Fannounce&tr=udp%3A%2F%2Ftracker.torrent.eu.org%3A451%2Fannounce&tr=udp%3A%2F%2Ftracker.cyberia.is%3A6969%2Fannounce&tr=udp%3A%2F%2F9.rarbg.to%3A2710%2Fannounce&tr=udp%3A%2F%2Fexodus.desync.com%3A6969%2Fannounce&tr=udp%3A%2F%2Fexplodie.org%3A6969%2Fannounce&tr=udp%3A%2F%2Ftracker.moeking.me%3A6969%2Fannounce&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337%2Fannounce&tr=udp%3A%2F%2Ftracker.tiny-vps.com%3A6969%2Fannounce&tr=udp%3A%2F%2Fipv4.tracker.harry.lu%3A80%2Fannounce&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969%2Fannounce&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969%2Fannounce&tr=udp%3A%2F%2Fopen.stealth.si%3A80%2Fannounce&tr=udp%3A%2F%2Ftracker.zer0day.to%3A1337%2Fannounce&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969%2Fannounce&tr=udp%3A%2F%2Fcoppersurfer.tk%3A6969%2Fannounce\"\n",
    "print(link)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 228
    },
    "id": "xaqIhXvlY60O",
    "outputId": "6b0eb6e9-91bb-4b6d-870f-e16802cdd8fe"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2020-10-05 15:44:57.999828\n",
      "Downloading Metadata...\n",
      "MataData Imported, Attempting Download\n",
      "Starting, PLease Wait\n",
      " [FTUApps.com] - MediaMonkey Gold 4.1.30.1913 (x86 & x64) Multilingual + Keygen\n",
      "0.01% complete (down: 0.4 kb/s up: 0.2 kB/s peers: 6) downloading \n",
      "15.02% complete (down: 559.1 kb/s up: 19.3 kB/s peers: 12) downloading \n",
      "92.83% complete (down: 2022.1 kb/s up: 61.4 kB/s peers: 11) downloading \n",
      "[FTUApps.com] - MediaMonkey Gold 4.1.30.1913 (x86 & x64) Multilingual + Keygen COMPLETE, Go to the Drive to Find the Downloaded File\n",
      "Elapsed Time:  0 min : 17 sec\n",
      "Closing Session, Come Back Again\n",
      "2020-10-05 15:45:15.023422\n"
     ]
    }
   ],
   "source": [
    "handle = lt.add_magnet_uri(ses, link, params) # Adding the Torrent File\n",
    "ses.start_dht()\n",
    "\n",
    "begin = time.time()\n",
    "print(datetime.datetime.now())\n",
    "\n",
    "print ('Downloading Metadata...')\n",
    "while (not handle.has_metadata()):\n",
    "    time.sleep(1)\n",
    "print ('MataData Imported, Attempting Download')\n",
    "\n",
    "print(\"Starting, PLease Wait\\n\", handle.name())\n",
    "\n",
    "# Print the various Information regarding Torrent\n",
    "while (handle.status().state != lt.torrent_status.seeding):\n",
    "    s = handle.status()\n",
    "    state_str = ['queued', 'checking', 'downloading metadata', \\\n",
    "            'downloading', 'finished', 'seeding', 'allocating']\n",
    "    print ('%.2f%% complete (down: %.1f kb/s up: %.1f kB/s peers: %d) %s ' % \\\n",
    "            (s.progress * 100, s.download_rate / 1000, s.upload_rate / 1000, \\\n",
    "            s.num_peers, state_str[s.state]))\n",
    "    time.sleep(5)\n",
    "\n",
    "end = time.time()\n",
    "print(handle.name(), \"COMPLETE, Go to the Drive to Find the Downloaded File\")\n",
    "\n",
    "# Compute and Print the Time Elapsed\n",
    "print(\"Elapsed Time: \",int((end-begin)//60),\"min :\", int((end-begin)%60), \"sec\")\n",
    "print(\"Closing Session, Come Back Again\")\n",
    "print(datetime.datetime.now())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "nd5ARYFFbRA5"
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "colab": {
   "name": "TorrentToDrive.ipynb",
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}